﻿namespace PowerSong.UserInterface
    {
    using System.Windows;
    using PowerSong.SongDatabase;
    using System;
    using System.Linq;
    using System.Collections.Generic;

    /// <summary>
    /// Interaction logic for EditCategoryWindow.xaml
    /// </summary>
    public partial class EditCategoryWindow : Window
        {

        private Category category = null;

        public EditCategoryWindow(Category category)
            {

            InitializeComponent();
            this.category = category;
            NameTextBox.Text = category.Name;
            DefaultCheckBox.IsChecked = category.IsDefault;

            // Show all songs assigned this category
            UpdateList();

            }

        private void UpdateList()
            {
            SongsListBox.Items.Clear();
            var songs = State.Database.Categories.GetSongs(category).OrderBy(song => song.Title);
            foreach (var song in songs)
                {
                SongsListBox.Items.Add(song);
                }
            }

        private void CancelButton_Click(object sender, RoutedEventArgs e)
            {
            this.DialogResult = false;
            }

        private void OKButton_Click(object sender, RoutedEventArgs e)
            {

            try
                {

                // Ensure a name has been specified
                if (string.IsNullOrEmpty(NameTextBox.Text))
                    {
                    throw new Exception("Please specify a name for this category.");
                    }

                // Update category details
                category.Name = NameTextBox.Text;
                category.IsDefault = DefaultCheckBox.IsChecked == true;

                // Save the category
                State.Database.Categories.Write(category);

                // Close the window
                this.DialogResult = true;

                }
            catch (Exception ex)
                {
                Utilities.ShowErrorMessage("Error Saving Category Details", ex.Message);
                }

            }

        private void SongsListBox_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
            {
            RemoveButton.IsEnabled = SongsListBox.SelectedItems.Count > 0;
            }

        private void RemoveButton_Click(object sender, RoutedEventArgs e)
            {

            // Get the user to confirm this action
            if (Utilities.Confirm("Remove Category From Songs", "Are you sure you wish to change all of the selected songs so that they are no longer assigned this category?"))
                {
                var errorCount = 0;
                foreach (Song song in SongsListBox.SelectedItems)
                    {
                    if (song.Categories.Contains(category))
                        {

                        if (song.Categories.Count == 1)
                            {
                            errorCount += 1;
                            }
                        else
                            {

                            // Remove the category from the song
                            song.Categories.Remove(category);
                            State.Database.Songs.Write(song);

                            }

                        }
                    }

                // Update the song list
                UpdateList();

                // Were there any problems?
                if (errorCount > 0)
                    {
                    var message = string.Format("There {0} solely assigned this category, therefore this category was not removed from {2}.",
                                                errorCount == 1 ? "was a song that was" : "were " + errorCount +" songs that were", errorCount == 1 ? "song" : "songs", errorCount == 1 ? "that song" : "those songs");
                    Utilities.ShowErrorMessage("Remove Category From Songs", message);
                    }

                }

            }

        private void AddButton_Click(object sender, RoutedEventArgs e)
            {

            var window = new SelectSongsWindow(State.Database.Songs.GetAllSongs(), State.Database.Categories.GetSongs(category));
            if (window.ShowDialog() == true)
                {
                foreach (var song in window.SelectedSongs)
                    {
                    if (!song.Categories.Contains(category))
                        {
                        song.Categories.Add(category);
                        State.Database.Songs.Write(song);
                        }
                    }
                UpdateList();
                }
            }

        }

    }
